import time

def sinaStrToQuotation(sina_string):
    q = {}
    sp = sina_string.split(',')
    q['symbol'] = sp[0][4:12]
    q['stockName'] = sp[0][13:]
    if q['stockName'] == '':
        q['stockName'] = '未知'
    q['market'] = q['symbol'][:2]
    q['stockCode'] = q['symbol'][2:]
    q['date'] = sp[2]
    q['time'] = sp[1]
    timeArray = time.strptime(q['date']+' '+q['time'], "%Y-%m-%d %H:%M:%S")
    q['timestamp'] =  int(time.mktime(timeArray))
    q['preClosePrice'] = float(sp[3])
    if q['stockName'][0] == 'N':
        q['hardenPrice'] =float(round(q['preClosePrice']*1.44,2))
    else:
        q['hardenPrice'] =float(round(q['preClosePrice']*1.1,2))
    q['deHardenPrice'] = float(round(q['preClosePrice']*0.9,2))
    q['openPrice'] = float(sp[4])
    q['highPrice'] = float(sp[5])
    q['lowPrice'] = float(sp[6])
    q['nowPrice'] = float(sp[7])
    q['volume'] = int(sp[10])
    q['amount'] = float(sp[11])
    q['totalTradeVolume'] =  q['volume']
    q['totalTradeAmount'] = q['amount']
    q['askVolume'] = int(sp[14])
    q['bidVolume'] = int(sp[12])
    q['totalTrades'] = int(sp[9])
    q['orderNum'] = 10
    q['status'] = int(sp[8])
    q['asks'] = []
    q['bids'] = []
    for i in range(0,10):
        b = {
            "price":float(sp[26 + i]),
            "volume":int(sp[36 + i]),
        }
        q['bids'].append(b)
        a = {
            "price":float(sp[46 + i]),
            "volume":int(sp[56 + i]),
        }
        q['asks'].append(a)
    return q;


def strToQuotation(quotation_string):
    sp = quotation_string.split(',')
    q = {};
    q['symbol'] = sp[0]
    q['market'] = sp[0][:2]
    q['stockCode'] = sp[0][2:]
    q['stockName'] = sp[1]
    q['timestamp'] = int(sp[2])
    timeArray = time.localtime(q['timestamp'])
    q['time'] = time.strftime("%H:%M:%S", timeArray)
    q['date'] = time.strftime("%Y-%m-%d", timeArray)
    q['preClosePrice'] = float(sp[3])
    q['hardenPrice'] = float(sp[4])
    q['deHardenPrice'] = float(sp[5])
    q['openPrice'] = float(sp[6])
    q['highPrice'] = float(sp[7])
    q['lowPrice'] = float(sp[8])
    q['nowPrice'] = float(sp[9])
    q['volume'] = int(sp[10])
    q['amount'] = float(sp[11])
    q['totalTradeVolume'] = int(sp[10])
    q['totalTradeAmount'] = float(sp[11])
    q['askVolume'] = int(sp[12])
    q['bidVolume'] = int(sp[13])
    q['totalTrades'] = int(sp[14])
    q['orderNum'] = int(sp[15])
    q['status'] = int(sp[16])
    q['asks'] = []
    q['bids'] = []
    orderNum = q['orderNum']
    for i in range(0,orderNum*2,2):
        q['asks'].append({
            "volume":int(sp[17 + orderNum*2 +i ]),
            "price":float(sp[18 + orderNum*2 +i ])
        })
    for i in range(0,orderNum*2,2):
        q['bids'].append({
            "volume":int(sp[17 +i ]),
            "price":float(sp[18 +i ])
        })
    return q
    
    

#'600002'形式的股票代码code转为掘金api用的symbol 'SHSE.600002'
def code_to_juejin_symbol(code_str):
    if code_str.find('60') == 0:
        return 'SHSE.'+code_str
    elif code_str.find('00') == 0 or code_str.find('002') == 0 or code_str.find('300') == 0:
        return 'SZSE.'+code_str
    else:
        print('code_to_juejin_symbol:ilegal code:',code_str)

#根据股票代码，返回'所在交易所_板块'
#输入为 '600002' 形式的code
def get_exchange_section(code):
    exchange = ''
    seciton = ''
    if code.find('60') == 0:
        return 'sh_zhuban'
    elif code.find('00') == 0:
        return 'sz_zhuban'
    elif code.find('002') == 0 :
        return 'sz_zxb'
    elif code.find('300') == 0:
        return 'sz_cyb' 
    elif code.find('900') == 0:
        return 'sh_b'
    elif code.find('200') == 0:
        return 'sz_b'        
    elif code.find('400') == 0:
        return 'sanban'
    else:
        pass
        #print('errorcode:%s'%(code))
        return 'other'  #指数？
    
    
    
        